//
//  TabBarViewController.m
//  UITabBarItem
//
//  Created by My Mac on 2017/1/25.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "TabBarViewController.h"
#import "MessageViewController.h"
#import "ContactViewController.h"
#import "DynamicViewController.h"
@interface TabBarViewController ()

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setAllChildView];
}
 -(void)setAllChildView
 {
     //创建一个消息视图对象
     MessageViewController *messageView = [[MessageViewController alloc]init];
     [self addChildViewController:messageView tabBarItemTitle:@"消息" itemImgName:@"message" itemSelectedImgName:@"message_press"];
 
     //创建一个联系人视图对象
     ContactViewController *contactView = [[ContactViewController alloc]init];
     [self addChildViewController:contactView tabBarItemTitle:@"联系人" itemImgName:@"contact" itemSelectedImgName:@"contact_press"];
 
     //创建一个动态视图对象
     DynamicViewController *dynamicView = [[DynamicViewController alloc]init];
     [self addChildViewController:dynamicView tabBarItemTitle:@"动态" itemImgName:@"dynamic" itemSelectedImgName:@"dynamic_press"];
 }

 -(void)addChildViewController:(UIViewController *)viewController tabBarItemTitle:(NSString *)title itemImgName:(NSString *)itemImgName itemSelectedImgName:(NSString *)itemSelectedImgName
{
     /**
      * UITabBarItem类实现一个项目在UITabBar对象.
      * 也就是说UITabBar上的按钮其实就是多个UITabBarItem对象组成的
      *
      * Title: tabBar上item的标题
      * image: tabBar上item普通状态下的图片
      * selectedImage: tabBar上item选中状态下的图片
      * imageWithRenderingMode: item的图片始终绘制为原始状态
      */
     UITabBarItem *messageItem = [[UITabBarItem alloc]initWithTitle:title image:[UIImage imageNamed:itemImgName] selectedImage:[[UIImage imageNamed:itemSelectedImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
     //将设置好的item赋值给视图上的tabBarItem
     viewController.tabBarItem = messageItem;
     //创建一个导航栏对象，将视图对象作为导航栏的根控制器
     UINavigationController *navMessage = [[UINavigationController alloc]initWithRootViewController:viewController];
     //设置视图的导航栏标题
     viewController.navigationItem.title = title;
     //将导航栏对象作为子视图添加到TabBarController上
     [self addChildViewController:navMessage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
